package com.banshou.installs.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import com.banshou.installs.R;
import com.banshou.installs.constant.Config;
import com.banshou.installs.databinding.AddStaffBinding;
import com.banshou.installs.model.dao.RequestAddStaff;
import com.banshou.installs.model.dao.ResponseLogin;
import com.banshou.installs.model.net.Observer;
import com.banshou.installs.model.net.ResponseInfo;
import com.banshou.installs.model.net.RetrofitManager2;
import com.banshou.installs.model.net.RxUtil;
import com.banshou.installs.utils.DialogUtils;
import com.banshou.installs.utils.ToastUtil;
import com.blankj.utilcode.util.RegexUtils;

import okhttp3.ResponseBody;

/**
 * Created by cjq on 2018/1/2.
 * Email: stoic_yb@139.com
 * features:
 */

public class AddStaffActivity extends BaseActivity {
    private AddStaffBinding mBinding;
    private DialogUtils dialogUtils;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.add_staff);
        initView();
    }

    private void initView() {
        dialogUtils = new DialogUtils();
        mBinding.addemName.addTextChangedListener(nameWatcher);
        mBinding.forTel.addTextChangedListener(telWatcher);
        mBinding.next.setOnClickListener(clickListener);
        mBinding.title.pagerTitle.setText(R.string.self_addstaff);
        mBinding.title.back.setOnClickListener(view -> finish());
    }

    public View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (!RegexUtils.isMobileSimple(mBinding.forTel.getText().toString().trim())) {
                ToastUtil.toast(Config.RIGHTTEL);
                return;
            }
            RequestAddStaff addStaff = new RequestAddStaff(mBinding.forTel.getText().toString().trim(),"123456",1,mBinding.addemName.getText().toString().trim());
            dialogUtils.showAni(AddStaffActivity.this);
            RetrofitManager2.getInstance()
                    .create(ResponseInfo.class)
                    .addStaff(addStaff)
                    .compose(RxUtil.lifecycle(AddStaffActivity.this))
                    .subscribe(new Observer<ResponseLogin>() {
                        @Override
                        protected void onNextStep(ResponseLogin ResponseLogin) {
                            dialogUtils.hideAni();
                            Intent intent = new Intent(AddStaffActivity.this, StaffManagerActivity.class);
                            startActivity(intent);
                            finish();
                        }

                        @Override
                        protected void onErrorResult() {
                            dialogUtils.hideAni();
                        }
                    });
        }
    };

    public TextWatcher nameWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (TextUtils.isEmpty(mBinding.addemName.getText().toString()) || TextUtils.isEmpty(mBinding.forTel.getText().toString())) {
                mBinding.next.setEnabled(false);
                mBinding.next.setClickable(false);
            } else {
                mBinding.next.setEnabled(true);
                mBinding.next.setClickable(true);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };
    public TextWatcher telWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (TextUtils.isEmpty(mBinding.addemName.getText().toString()) || TextUtils.isEmpty(mBinding.forTel.getText().toString())) {
                mBinding.next.setEnabled(false);
                mBinding.next.setClickable(false);
            } else {
                mBinding.next.setEnabled(true);
                mBinding.next.setClickable(true);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };

}
